﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Yuebon.Commons.Helpers;
using Yuebon.Commons.Log;
using Yuebon.Commons.Models;
using Yuebon.Security.IServices;
using Yuebon.Security.Models;
using Yuebon.WebApi.Common;
using Yuebon.WebApi.Controllers;
using Yuebon.WebApi.Models;
using Yuebon.WebApi.ViewModels;

namespace Yuebon.WebApi.Areas.Security.Controllers
{
    /// <summary>
    /// 用户接口控制器
    /// </summary>
    [Route("api/Security/[controller]")]
    public class UserController: BusinessApiController<User, IUserService>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_iService"></param>
        public UserController(IUserService _iService) : base(_iService)
        {
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <param name="token">token令牌</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(string token)
        {
            CommonResult result = new CommonResult();
            try
            {
                result = CheckToken(token);
                if (result.ErrCode == ErrCode.successCode)
                {
                    IEnumerable<User> list = await iService.GetAllAsync();
                    List<UserView> resultlist = new List<UserView>();
                    foreach (User user in list)
                    {
                        UserView model = new UserView();
                        model.Account = user.Account;
                        model.Birthday = user.Birthday;
                        model.Email = user.Email;
                        model.Gender = user.Gender;
                        model.HeadIcon = user.HeadIcon;
                        model.Id = user.Id;
                        model.IsAdministrator = user.IsAdministrator;
                        model.ManagerId = user.ManagerId;
                        model.MobilePhone = user.MobilePhone;
                        model.NickName = user.NickName;
                        model.OrganizeId = user.OrganizeId;
                        model.RealName = user.RealName;
                        model.RoleId = user.RoleId;
                        model.SecurityLevel = user.SecurityLevel;
                        model.Signature = user.Signature;
                        model.WeChat = user.WeChat;
                        resultlist.Add(model);
                    }
                    return ToJsonContent(resultlist);
                }
            }catch(Exception ex)
            {
                var type = MethodBase.GetCurrentMethod().DeclaringType;
                Log4NetHelper.WriteError(type, ex);
            }
            return ToJsonContent(result);
        }
        /// <summary>
        /// 根据用户Id获取用户信息
        /// </summary>
        /// <param name="id">主键Id</param>
        /// <param name="token">token令牌</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> Get(string id, string token)
        {
            CommonResult result = new CommonResult();
            
            result = CheckToken(token);
            if (result.ErrCode == ErrCode.successCode)
            {
                User user = await iService.GetAsync(id);
                if (user != null)
                {
                    UserView model = new UserView();
                    model.Account = user.Account;
                    model.Birthday = user.Birthday;
                    model.Email = user.Email;
                    model.Gender = user.Gender;
                    model.HeadIcon = user.HeadIcon;
                    model.Id = user.Id;
                    model.IsAdministrator = user.IsAdministrator;
                    model.ManagerId = user.ManagerId;
                    model.MobilePhone = user.MobilePhone;
                    model.NickName = user.NickName;
                    model.OrganizeId = user.OrganizeId;
                    model.RealName = user.RealName;
                    model.RoleId = user.RoleId;
                    model.SecurityLevel = user.SecurityLevel;
                    model.Signature = user.Signature;
                    model.WeChat = user.WeChat;
                    return ToJsonContent(model);
                }
                else
                {
                    result.ErrMsg = ErrCode.err50001;
                    result.ErrCode = "50001";
                }
            }
            return ToJsonContent(result);
        }




    }
}
